/**
 * Created by Administrator on 2019/1/26.
 */
'use strict';
const Controller = require('egg').Controller;

class LoginController extends Controller {

  /**
   * 登陆并且直接下载文件
   */
  async loginAndDown() {
    const { ctx } = this;
    const userInfo = ctx.request.body;

    const data = await ctx.service.user.index(userInfo);

    if (data.err) { // 处理报错
      ctx.body = ctx.helper.sendErr(data.err);
      return false;
    }

    if (Array.isArray(data) && data.length) {
      const thead = [ '部门', '工号', '姓名', '工作日期', '最早签到时间', '最晚签到时间', '工作时长', '加班餐费', '加班的士费', '乘车路线' ];
      const table = [ thead, ...data ];
      const name = userInfo.startDate + '-' + userInfo.endDate;
      const colWidth = [ 20, 10, 8, 20, 14, 14, 10, 10, 11, 22 ];

      const path = ctx.helper.saveExcel(table, name, colWidth, 'origin');
      ctx.body = {
        code: 0,
        data: path,
      };
    } else {
      ctx.body = ctx.helper.sendErr('没有加班数据');
    }
  }
}

module.exports = LoginController;
